"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 服务端工具
Case Name   : 备份结果不包含依赖于指定对象的对象信息（--exclude-self）
Create At   : 2021/2/7
Owner       : opentestcase002
Description :
    1.创建数据库
    2.创建数据
    3.备份结果不包含依赖于指定对象的对象信息（--exclude-self）
    4.连接数据库，清理环境
Expect      :
    1.创建数据库成功
    2.创建数据成功
    3.导出成功
    4.清理环境成功
History     :
    modify by opentestcase002 20220811:研发代码变更，优化断言
    modify by opentestcase002 20221130:研发代码变更，优化断言
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('dbuser')
        self.db_name = 'db_dump_case0067'
        self.t_name1 = 't_dump_case0067_01'
        self.t_name2 = 't_dump_case0067_02'
        self.v_name = 'v_dump_case0067'
        self.sch_name1 = 'sch_dump_case0067_01'
        self.sch_name2 = 'sch_dump_case0067_02'
        self.constant = Constant()

    def test_server_tools(self):
        text = '-----step1:连接数据库并创建数据库,expect:成功-----'
        self.log.info(text)
        sql_cmd1 = f'''drop database if exists {self.db_name};
            create database {self.db_name};
                   '''
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.log.info(msg1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, msg1,
                      '执行失败:' + text)

        text = '-----step2:在创建好的数据库中创建模式，创建表\
        并插入数据并设置表的所属模式,expect:成功-----'
        self.log.info(text)
        sql_cmd2 = f''' drop schema if exists {self.sch_name1};
            drop schema if exists {self.sch_name2};
            create schema  {self.sch_name1};
            create schema  {self.sch_name2};
            drop table if  exists {self.t_name1};
            drop table if  exists {self.t_name2};
            create table {self.t_name1} (id int);
            insert into {self.t_name1} values(1),(2),(3);
            create table {self.t_name2} (id int);
            insert into {self.t_name2} values(8),(2),(5);
            alter table {self.t_name1} set schema {self.sch_name1};
            alter table {self.t_name2} set schema {self.sch_name2};
            drop view if exists view_{self.t_name1};
            create view {self.v_name} as select * from 
            {self.sch_name1}.{self.t_name1};
            '''
        msg2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(msg2)
        self.assertEqual(2, msg2.count('CREATE SCHEMA'), '执行失败:' + text)
        self.assertEqual(2, msg2.count('CREATE TABLE'), '执行失败:' + text)
        self.assertEqual(2, msg2.count('INSERT'), '执行失败:' + text)
        self.assertEqual(2, msg2.count('ALTER TABLE'), '执行失败:' + text)
        self.assertEqual(1, msg2.count('CREATE VIEW'), '执行失败:' + text)

        text = '-----step3:备份结果不包含指定对象自身的信息,expect:成功-----'
        self.log.info(text)
        excute_cmd3 = f'source {macro.DB_ENV_PATH} ;' \
            f'gs_dump ' \
            f'-p {self.dbuser_node.db_port} {self.db_name}  ' \
            f'-F p  ' \
            f'--exclude-self ' \
            f'--include-depend-objs ' \
            f'-t {self.sch_name1}.{self.t_name1}; '
        self.log.info(excute_cmd3)
        msg3 = self.dbuser_node.sh(excute_cmd3).result()
        self.log.info(msg3)
        self.assertIn('DROP VIEW IF EXISTS', msg3, '执行失败:' + text)

    def tearDown(self):
        text = '-----step4:清理环境,expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f'''drop database if exists {self.db_name}; '''
        msg4 = self.sh_primy.execut_db_sql(sql_cmd4)
        self.log.info(msg4)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, msg4,
                      '执行失败:' + text)
        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)
